package com.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.pojo.MovieList;
import com.pojo.ProgramCategory;
import com.pojo.Special;
import com.pojo.SpecialFilmList;
import com.service.DyService;
import com.service.DyTypeService;
import com.service.SpecialFilmListService;
import com.service.SpecialService;
import com.util.DesUtil;
import com.util.HardUtil;
import com.util.MD5Util;
import com.util.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("ALL")
@Component("TimeTaskUpdateDyTypeService")
public class TimeTaskUpdateDyTypeService {


    @Value("${server.port}")
    private String port;


    @Autowired
    private DyTypeService dyTypeService;

    @Autowired
    private DyService dyService;

    @Value("${server.url}")
    private String server;

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private SpecialService specialService;

    @Autowired
    private SpecialFilmListService specialFilmListService;

    @Autowired

    @Value("${dy.tmp}")
    private String tmp;

    @Value("${dy.home}")
    private String home;


    private List<ProgramCategory> removeAllFilmType(List<ProgramCategory> src, List<ProgramCategory> target) {
        LinkedList<ProgramCategory> result = new LinkedList<ProgramCategory>(src); //大集合用linkedlist
        HashSet<ProgramCategory> targetHash = new HashSet<ProgramCategory>(target); //小集合用hashset
        Iterator<ProgramCategory> iter = result.iterator(); //采用Iterator迭代器进行数据的操作

        while (iter.hasNext()) {
            if (targetHash.contains(iter.next())) {
                iter.remove();
            }
        }
        return result;
    }


    /**
     * 更新电影类型
     */

    @Scheduled(cron = "12 6/5 *  * * ?")
    public synchronized void updateMovieType() {

        try {
            String getAllMovieType_url = server + "api/v1/getAllMovieType";
            String response_film_type = restTemplate.getForObject(getAllMovieType_url, String.class);
            JSONObject jsonObject_film_type = JSONObject.parseObject(response_film_type);

            if (jsonObject_film_type != null) {
                int code = jsonObject_film_type.getInteger("code");
                if (code != 200) {
                    System.out.println(jsonObject_film_type.getString("msg"));
                    return;
                }


                //本地电影类型
                List<ProgramCategory> localFilmType = dyTypeService.list();
                //获取电影类型
                List<ProgramCategory> remoteFilmType = jsonObject_film_type.parseArray(jsonObject_film_type.getString("data"), ProgramCategory.class);

                //要新增的电影类别
                List<ProgramCategory> addFilmType = removeAllFilmType(remoteFilmType, localFilmType);
                dyTypeService.saveBatch(addFilmType);
                //要删除的电影类别
                List<ProgramCategory> removeFilmType = removeAllFilmType(localFilmType, remoteFilmType);
                for (ProgramCategory s : removeFilmType) {
                    dyTypeService.removeById(s.getId());
                }


                //专题更新
                List<ProgramCategory> list = dyTypeService.list();
                for (ProgramCategory s : list) {
                    String id = s.getId();
                    for (ProgramCategory r : remoteFilmType) {
                        if (id.equals(r.getId())) {
                            BeanUtil.copyProperties(r, s);
                            dyTypeService.updateById(s);
                            break;
                        }
                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
